<?php
class Australian_Controller_Action extends Zend_Controller_Action{
	public $_lang;
	public function init(){
		parent::init();
// 		$template_path = TEMPLATE_PATH . "/public/system";
// 		$this->loadTemplate($template_path, 'template.ini', 'template');
	}
	
	protected function loadTemplate($template_path, $fileConfig = 'template.ini', $sectionConfig = 'template'){
		
		// Xoa nhung du lieu cua layout truoc do
		$this->view->headTitle()->set('');
		$this->view->headMeta()->getContainer()->exchangeArray(array());
		$this->view->headLink()->getContainer()->exchangeArray(array());
		$this->view->headScript()->getContainer()->exchangeArray(array());
		
		$filename = $template_path . "/" . $fileConfig;
		$section = $sectionConfig;
		$config = new Zend_Config_Ini($filename, $section);
		$config = $config->toArray();
		
		$baseUrl = $this->_request->getBaseUrl();
		$templateUrl = $baseUrl . $config['url'];
		$cssUrl = $templateUrl . $config['dirCss'];
		$jsUrl 	= $templateUrl . $config['dirJs'];
		$imgUrl = $templateUrl . $config['dirImg'];
                $lang = $this->getRequest()->getParam('lang');
                $this->_lang = $lang;
//                echo $lang;
		
		// Nap title cho layout
		$this->view->headTitle($config['title']);
		
		// Nap cac the mete vao layout
		if(count($config['metaHttp']) > 0){
			foreach($config['metaHttp'] as $key => $value){
				$tmp = explode("|", $value);
				$this->view->headMeta()->appendHttpEquiv($tmp[0], $tmp[1]);
			}
		}
		if(count($config['metaName']) > 0){
			foreach($config['metaName'] as $key => $value){
				$tmp = explode("|", $value);
				$this->view->headMeta()->appendName($tmp[0], $tmp[1]);
			}
		}
		
		// Nap cac tap tin css vao layout
		if(count($config['fileCss']) > 0){
			foreach($config['fileCss'] as $key => $css){
				$this->view->headLink()->appendStylesheet($cssUrl . $css, 'screen');
			} 
		}
		
		// Nap cac tap tin javascript vao layout
		if(count($config['fileJs']) > 0){
			foreach($config['fileJs'] as $key => $js){
				$this->view->headScript()->appendFile($jsUrl . $js, 'text/javascript');
			}
		}
		
		// Xuat ra view
		$this->view->templateUrl = $templateUrl;
		$this->view->cssUrl = $cssUrl;
		$this->view->jsUrl 	= $jsUrl;
		$this->view->imgUrl = $imgUrl;
                $this->view->lang = $lang;
			
	
		$option = array('layoutPath' => $template_path, 'layout' => $config['layout']);
		Zend_Layout::startMvc($option);
	}
}